package Pet.Management.service.impl;

import Pet.Management.domain.entity.User;
import Pet.Management.repository.UserRepository;
import Pet.Management.service.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public boolean isUserExist(String username) {
        return userRepository.findByUsername(username).isPresent();
    }

    @Override
    @Transactional
    public User registerUser(User user) {
        // 1. 设置创建时间和更新时间
        LocalDateTime now = LocalDateTime.now();
        user.setCreateTime(now);
        user.setUpdateTime(now);

        // 2. 设置默认值
        if (user.getStatus() == null) {
            user.setStatus((byte) 0); // 正常状态
        }
        if (user.getIsAdmin() == null) {
            user.setIsAdmin((byte) 0); // 普通用户
        }

        // 3. 保存用户
        return userRepository.save(user);
    }
} 